package com.icebergteam.warfacewidget.data.sql;

import java.util.ArrayList;
import java.util.Date;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;
import com.icebergteam.warfacewidget.videos.data.VideosItem;

public class VideosTable extends BasicTable {
	public static String className = VideosTable.class.getCanonicalName();
	// Constants
	// Table
	public static final String TABLE_NAME	= "videos";
	// Fields
	public static final String FIELD_ID				= "id ";
	public static final String FIELD_URL			= "url ";
	public static final String FIELD_HTML			= "html ";
	public static final String FIELD_TITLE = "title ";
	public static final String FIELD_PUBDATE = "pubdate ";
	public static final String FIELD_IMGSRC = "imgsrc ";
	public static final String FIELD_READ = "read ";
	public static final String FIELD_VIDEO_URL = "video_url ";

	

	public VideosTable() {
		
	}
	
	public String createTableStr() {
		LogManager.LogV(className, "createTable");
		String field_id			= FIELD_ID		+ "integer primary key autoincrement not null";
		String field_url		= FIELD_URL		+ "text";
		String field_html		= FIELD_HTML	+ "text";
		String field_title 		= FIELD_TITLE   + "text";
		String field_pubdate 	= FIELD_PUBDATE	+ "integer";
		String field_imgsrc 	= FIELD_IMGSRC	+ "text";
		String field_read		= FIELD_READ 	+ "BOOLEAN";
		String field_video_url		= FIELD_VIDEO_URL		+ "text";
		
		return AppManager.getDatabase().createTableStr(TABLE_NAME, field_id, field_url, field_html, field_title, field_pubdate, field_imgsrc, field_read, field_video_url);
	
	}
	
	
	public Cursor getVideosByUrl(String url) throws SQLException{
	
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
				return db.query(
					VideosTable.TABLE_NAME, 
					new String[] { VideosTable.FIELD_TITLE, VideosTable.FIELD_URL, VideosTable.FIELD_PUBDATE,  VideosTable.FIELD_IMGSRC, VideosTable.FIELD_READ, VideosTable.FIELD_VIDEO_URL}, 
					String.format("%s = ?", VideosTable.FIELD_URL), 
					new String[]{url}, 
					null, 
					null,
					String.format("%s DESC limit %s", VideosTable.FIELD_PUBDATE, 70)
					);
			}
		} catch (Exception e) {
			LogManager.LogEA(VideosTable.className, ViewUtils.getErrorMessage(e));
		}
		return null;
	}
	
	public Cursor getVideos() throws SQLException{
		try {
			SQLiteDatabase db = AppManager.getDatabase().getWritableDatabase();
			if(db != null && db.isOpen()){
			
				return db.query(
					VideosTable.TABLE_NAME, 
					new String[] { VideosTable.FIELD_TITLE, VideosTable.FIELD_URL, VideosTable.FIELD_PUBDATE,  VideosTable.FIELD_IMGSRC, VideosTable.FIELD_READ, VideosTable.FIELD_VIDEO_URL},
					null,
					null,
					null, 
					null,
					String.format("%s DESC limit %s", VideosTable.FIELD_PUBDATE, 70)
					);
			}
		} catch (Exception e) {
			LogManager.LogEA(VideosTable.className, ViewUtils.getErrorMessage(e));
		}
		return null;
	}
		
	public Date getLastUpdate() throws SQLException {
		try {
			Cursor cur = AppManager.getDatabase().getWritableDatabase().rawQuery(
					String.format(
							"SELECT MAX (%s) AS \"max_date\"  " +
							"FROM %s",VideosTable.FIELD_PUBDATE.trim(),  VideosTable.TABLE_NAME),
							null);
			cur.moveToFirst();
			if(cur.getString(0) == null){
				return new Date();
			}
			return new Date(Long.parseLong(cur.getString(0)));
		} catch (Exception e) {
			LogManager.LogEA(VideosTable.className, ViewUtils.getErrorMessage(e));
		}
		return null;
	}
	
	public ArrayList<VideosItem> getVideosList(){
		Cursor cursor = AppManager.getDatabase().getVideosTable().getVideos();
		ArrayList<VideosItem> listVideosItems = new ArrayList<VideosItem>();
		while(cursor!= null && cursor.moveToNext()){
			VideosItem item = new VideosItem();
			item.setTitle(cursor.getString(cursor.getColumnIndex(VideosTable.FIELD_TITLE.trim())));
			item.setPubdate(cursor.getLong(cursor.getColumnIndex(VideosTable.FIELD_PUBDATE.trim())));
			item.setImgSrc(cursor.getString(cursor.getColumnIndex(VideosTable.FIELD_IMGSRC.trim())));
			item.setUrl(cursor.getString(cursor.getColumnIndex(VideosTable.FIELD_URL.trim())));
			item.setVideoUrl(cursor.getString(cursor.getColumnIndex(VideosTable.FIELD_VIDEO_URL.trim())));
			item.setRead(cursor.getInt(cursor.getColumnIndex(VideosTable.FIELD_READ.trim())));
			
			listVideosItems.add(item);
		}
		return listVideosItems;
	}
	
	
	
	@Override
	protected WhereWrapper getUpdateWhere(ContentValues cv) {
		WhereWrapper where = new WhereWrapper();
		where.setWhere( String.format("%s=?", VideosTable.FIELD_URL));
		where.addValue(cv.getAsString(VideosTable.FIELD_URL));
		
		return where;
	}
	
	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}
}